using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

using PersistenceObject.Framework;
using PersistenceObject.Framework.Expressions;

using FundSms.Server.Model;

namespace FundSms.Server.Monitor
{
    public class ChannelHelper
    {
        private static readonly string DataKey = "Sms";

        public static IList<ChannelQueue> GetChannel()
        {
            IList<ChannelQueue> listqueue = new List<ChannelQueue>();
            ChannelQueue queue = null;

            IList<Channel> list = QueryOperate<Channel>.Create(DataKey).OrderBy(Channel._ChannelID).List();

            foreach (Channel c in list)
            {
                queue = new ChannelQueue();

                queue.ChannelCode = c.ChannelCode;
                queue.ChannelName = c.ChannelName;
                queue.ChannelType = c.ChannelTypeCode;

                using (DataSession ds = new DataSession(DataKey))
                {
                    ds.Open();

                    IDbConnection conn = ds.Connection;

                    System.Data.SqlClient.SqlCommand command = conn.CreateCommand() as System.Data.SqlClient.SqlCommand;
                    command.CommandText = "SELECT COUNT(1) FROM SMS_Channel_" + queue.ChannelCode;
                    command.CommandType = CommandType.Text;

                    try
                    {
                        int result = Convert.ToInt32(ds.ExecuteScalar(command));

                        queue.QueueCount = result;
                    }
                    catch
                    {
                        queue.QueueCount = 0;
                    }

                    ds.Close();
                }

                listqueue.Add(queue);

            }

            return listqueue;
        }
    }

    public class ChannelQueue
    {
        private string channelcode;

        public string ChannelCode
        {
            get { return channelcode; }
            set { channelcode = value; }
        }
        private string channelname;

        public string ChannelName
        {
            get { return channelname; }
            set { channelname = value; }
        }
        private int queuecount;

        public int QueueCount
        {
            get { return queuecount; }
            set { queuecount = value; }
        }

        private string channeltype;
        public string ChannelType
        {
            get
            {
                return this.channeltype;
            }
            set
            {
                this.channeltype = value;
            }
        }
    }
}
